/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package action;

import dao.ProductDAO;
import dao.mysql.ProductDAOImpl;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import model.Product;
import model.Transaksi;
import model.TransaksiDetail;
import utility.DBConnection;

/**
 *
 * @author Student
 */
public class InvoiceChooseProductAction implements Action {

    @Override
    public String execute(HttpServletRequest request) {
        String url = "";
        int quantity = Integer.valueOf(request.getParameter("quantity"));
        int id = Integer.valueOf(request.getParameter("id"));
        DBConnection db = new DBConnection();
        ProductDAO dao = new ProductDAOImpl(db.getConnection());
        HttpSession sesi = request.getSession(true);
        try{
            Product product = dao.getProductById(id);            
            Transaksi invoice = (Transaksi)sesi.getAttribute("invoice");
            TransaksiDetail detail = new TransaksiDetail();
            detail.setPrice(product.getPrice());
            detail.setQuantity(quantity);
            detail.setProduct(product);
            invoice.getDetails().add(detail);
            sesi.setAttribute("invoice", invoice);
            
            url = "invoice_new_2.jsp";
        }catch(Exception ex){
            url = "errors.jsp";      
            sesi.removeAttribute("invoice");
            request.setAttribute("message", ex.getMessage());
        }
        return url;
    }
}
